package com.siwei.mes.entity.erp;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.siwei.mes.common.util.ConvertUtils;
import com.siwei.mes.common.util.DateHelper;
import com.siwei.mes.common.util.JsonUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;

import javax.persistence.Transient;
import java.util.Arrays;
import java.util.List;

/**
 * 任务单额外信息实体
 *
 * @Description: 任务单额外信息实体
 * @Version: 1.0
 * @author: mlchen
 * @date: 2024/8/2 14:22
 */
@Slf4j
@Data
public class RwdextraInfo {
    @JSONField(name = "FRwdh")
    @ApiModelProperty(value = "任务单号id")
    private String FRwdh;
    @JSONField(name = "FHtbh")
    @ApiModelProperty(value = "合同编号")
    private String FHtbh;
    @JSONField(name = "FRwxz")
    @JsonProperty(value = "fRwxz")
    @ApiModelProperty(value = "任务性质")
    private String FRwxz;
    @JSONField(name = "FRwly")
    @ApiModelProperty(value = "任务来源")
    private String FRwly;
    @JSONField(name = "FZt")
    @ApiModelProperty(value = "状态")
    private String FZt;
    @JSONField(name = "FHtdw")
    @ApiModelProperty(value = "单位名称")
    private String FHtdw;
    @JSONField(name = "FGcmc")
    @ApiModelProperty(value = "工程名称")
    private String FGcmc;
    @JSONField(name = "FJzbw")
    @ApiModelProperty(value = "浇筑部位")
    private String FJzbw;
    @JSONField(name = "FJzfs")
    @ApiModelProperty(value = "浇筑方式")
    private String FJzfs;
    @JSONField(name = "FGcdz")
    @ApiModelProperty(value = "施工地点")
    private String FGcdz;
    @JSONField(name = "FGls")
    @ApiModelProperty(value = "运距")
    private String FGls;
    @JSONField(name = "FJhrq")
    @ApiModelProperty(value = "计划日期")
    private String FJhrq;
    @JSONField(name = "FTpz")
    @ApiModelProperty(value = "砼品种")
    private String FTpz;
    @JSONField(name = "FTld")
    @ApiModelProperty(value = "坍落度")
    private String FTld;
    @JSONField(name = "FSnbh")
    @ApiModelProperty(value = "水泥品种")
    private String FSnbh;
    @JSONField(name = "FSzgg")
    @ApiModelProperty(value = "石子规格")
    private String FSzgg;
    @JSONField(name = "FTbj")
    @ApiModelProperty(value = "备注|渗料规格|外加剂规格|抗渗等级|其他要求|施工单位联系人|工程编号|营销部门联系人|监督号码|优先采用的仓库编号|监控系统工地号|质检员")
    private String FTbj;
    @JSONField(name = "FJhsl")
    @ApiModelProperty(value = "计划方量")
    private String FJhsl;
    @JSONField(name = "FScbt")
    @ApiModelProperty(value = "生产拌台")
    private String FScbt;
    @JSONField(name = "FWcsl")
    @ApiModelProperty(value = "完成方量/砼数量")
    private String FWcsl;
    @JSONField(name = "FLjcs")
    @ApiModelProperty(value = "累计车数")
    private String FLjcs;
    @JSONField(name = "FCzy")
    @ApiModelProperty(value = "操作员")
    private String FCzy;
    @JSONField(name = "FSgpb")
    @ApiModelProperty(value = "施工配合比号")
    private String FSgpb;
    @JSONField(name = "FSjpb")
    @ApiModelProperty(value = "砂浆配合比号")
    private String FSjpb;
    @JSONField(name = "FRwno")
    @ApiModelProperty(value = "任务单号")
    private String FRwno;
    @JSONField(name = "FHtno")
    @ApiModelProperty(value = "合同号")
    private String FHtno;
    @JSONField(name = "FJbsj")
    @ApiModelProperty(value = "搅拌时间")
    private String FJbsj;
    @JSONField(name = "FBSfs")
    @ApiModelProperty(value = "泵送方式")
    private String FBSfs;
    @JSONField(name = "FClsjNo")
    @ApiModelProperty(value = "出砼门控制参数")
    private String FClsjNo;
    @JSONField(name = "FPhbNo")
    @ApiModelProperty(value = "配合比号")
    private String FPhbNo;
    @JSONField(name = "FPhbId")
    @ApiModelProperty(value = "配合比id")
    private String FPhbId;
    @JSONField(name = "FA4")
    @ApiModelProperty(value = "FA4")
    private String FA4;
    @JSONField(name = "updateTime")
    @ApiModelProperty(value = "修改时间")
    private String updateTime;

    @JSONField(name = "phbshowid")
    @ApiModelProperty(value = "施工配比id（品控）")
    private Integer phbshowid;

    @JSONField(name = "phbshow")
    @ApiModelProperty(value = "施工配比编号（品控）")
    private String phbshow;

    @Transient
    @ApiModelProperty(value = "抗渗等级 来源在FTbj中")
    private String ksdj;

    @Transient
    @ApiModelProperty(value = "抗氯离子  来源在FTbj中")
    private String kllz;

    /**
     * json转对象
     *
     * @param str
     * @return
     */
    public static RwdextraInfo jsonToObj(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        }
        boolean jsonString = JsonUtils.isJSONString(str);
        if (!jsonString) {
            return null;
        }
        String info = StringEscapeUtils.unescapeJava(str);
        RwdextraInfo rwdextraInfo = JsonUtils.getObject(info, RwdextraInfo.class);
        if (rwdextraInfo != null) {
            /**这里解析Ftbj 获取抗渗等级*/
            String fTbj = rwdextraInfo.getFTbj();
            rwdextraInfo.setKllz(getFtbjValueKllz(fTbj));

            rwdextraInfo.setKsdj(ConvertUtils.getMesTpzToKs(rwdextraInfo.getFTpz()));
        }
        return rwdextraInfo;
    }

    /**
     * 解析Ftbj |截取之后，通过CL前缀获取氯离子含量
     *
     * @param str ftbj字符串
     * @return
     */
    public static String getFtbjValueKllz(String str) {
        try {
            if (StringUtils.isBlank(str)) {
                return null;
            }
            String[] tbjList = str.split("\\|");
            for (String tbj : tbjList) {
                String kllz = ConvertUtils.getMesTpzToKllz(tbj);
                if (StringUtils.isNotBlank(kllz)) {
                    return kllz.replace("CL", "");
                }
            }
            return null;
        } catch (Exception e) {
            log.info("解析Ftbj |截取之后，通过CL前缀获取氯离子含量出错返回null");
            return null;
        }
    }

    /**
     * 解析Ftbj获取对应下标的数据
     *
     * @param str   ftbj字符串
     * @param index 下标
     * @return
     */
    public static String getFtbjValue(String str, int index) {
        try {
            if (StringUtils.isBlank(str)) {
                return null;
            }
            List<String> tbjList = Arrays.asList(str.split("\\|"));
            if (tbjList.isEmpty()) {
                return null;
            }
            int size = tbjList.size();
            /**集合长度如果小于获取数据的下标*/
            if (size < index) {
                return null;
            }
            return tbjList.get(index);
        } catch (Exception e) {
            log.info("解析ERP任务单扩展信息中Ftbj字段出错，Ftbj：【{}】", str);
            return null;
        }
    }

    public static void main(String[] args) {
        System.out.println("\\|");

        String str = "|||P20|细石,不加水,特殊要求|欧巴桑|||13856456345||||";
        String ftbjValue = getFtbjValue(str, 3);
        System.out.println(ftbjValue);

        /**组装任务单扩展信息*/
        RwdextraInfo rwdextraInfo = new RwdextraInfo();
        rwdextraInfo.setFRwdh("RW001");
        rwdextraInfo.setFHtbh("20231108001");
        rwdextraInfo.setFRwxz("");
        rwdextraInfo.setFRwly("超级调度");
        rwdextraInfo.setFZt("正在生产");
        rwdextraInfo.setFHtdw("黄兴公园");
        rwdextraInfo.setFGcmc("工程名称");
        rwdextraInfo.setFJzbw("泰和路高架外圈第3,4车道");
        rwdextraInfo.setFJzfs("非泵");
        rwdextraInfo.setFGcdz("上海市杨浦区政立路467号3");
        rwdextraInfo.setFGls("0");
        rwdextraInfo.setFJhrq(DateHelper.getCurrentDateTime());
        rwdextraInfo.setFTpz("C30");
        rwdextraInfo.setFTld("100±30");
        rwdextraInfo.setFSnbh("");
        rwdextraInfo.setFSzgg("");
        rwdextraInfo.setFTbj("|||||外人田1|||13843254354||||");
        rwdextraInfo.setFJhsl("888");
        rwdextraInfo.setFScbt("1号拌台");
        rwdextraInfo.setFWcsl("123");
        rwdextraInfo.setFLjcs("");
        rwdextraInfo.setFCzy("模拟");
        rwdextraInfo.setFSgpb("");
        rwdextraInfo.setFSjpb("");
        rwdextraInfo.setFRwno("RWOO1");
        rwdextraInfo.setFHtno("");
        rwdextraInfo.setFJbsj("");
        rwdextraInfo.setFBSfs("");
        rwdextraInfo.setFClsjNo("");
        rwdextraInfo.setFPhbNo("PHB001");
        rwdextraInfo.setPhbshow("PHB001");
        rwdextraInfo.setFPhbId("");
        rwdextraInfo.setPhbshowid(null);
        rwdextraInfo.setFA4("");
        rwdextraInfo.setUpdateTime(DateHelper.getCurrentDateTime());

        String jsonString = JSONObject.toJSONString(rwdextraInfo);
        System.out.println(jsonString);


        String kllzStr = "||||CL0.3||||||||";
        System.out.println(getFtbjValue(kllzStr, 4));
    }
}
